package com.moduleSys.roleMenuService;

import com.moduleSys.entity.system.RoleMenu;
import com.moduleSys.mapper.RoleMenuMapper;
import com.moduleSys.roleMenuService.RoleMenuService;
import com.moduleSys.utils.json.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by czw on 2017/7/22 0022.
 */
@Service
public class RoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Transactional(propagation= Propagation.REQUIRED,rollbackFor={RuntimeException.class, Exception.class})
    public JsonResult insert(Long roleId, Long[] mId) {
        List<RoleMenu> menuIdList = new ArrayList<>();
        roleMenuMapper.deleteByRoleId(roleId);
        if ( mId != null && mId.length > 0){
            for ( Long roleMenuId : mId){
                menuIdList.add(new RoleMenu(roleId, roleMenuId));
            }
            roleMenuMapper.insertRoleMenuForeach(menuIdList);
            return new JsonResult(1,null,null);
        }else{
            return new JsonResult(0,null,null);
        }

    }
}
